//
//  XJ_HomeNewsViewController.m
//  XunJieProject
//
//  Created by 薛子 on 2018/6/11.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_HomeNewsViewController.h"
#import "XJ_HomeNewsCell.h"
#import "XJ_homeNewListModel.h"
@interface XJ_HomeNewsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView * newTabel;
@property (nonatomic, strong)NSMutableArray * newsArray;
@end


@implementation XJ_HomeNewsViewController


- (NSMutableArray *)newsArray{
    
    if (_newsArray == nil) {
        
        _newsArray = [NSMutableArray array];
    }
    return _newsArray;
}
- (UITableView *)newTabel{
    
    if (_newTabel == nil) {
        
        _newTabel = [[UITableView alloc]initWithFrame:CGRectMake(15, SafeAreaTopHeight, SCREEN_WIDTH-30, SCREEN_HEIGHT - SafeAreaTopHeight) style:UITableViewStylePlain];
        _newTabel.delegate = self;
        _newTabel.dataSource = self;
        _newTabel.separatorStyle = UITableViewCellSeparatorStyleNone;
        _newTabel.backgroundColor = [UIColor colorWithHexString:@"f2f2f1"];
        _newTabel.showsVerticalScrollIndicator =NO;
        
    }
    
    return _newTabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"消息中心";
    self.view.backgroundColor = [UIColor colorWithHexString:@"f2f2f1"];
    
    [self requestNewsListData];
    
    [self.view addSubview:self.newTabel];
    // Do any additional setup after loading the view.
}

#pragma tableDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.newsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, 10)];
    sectionView.backgroundColor = [UIColor colorWithHexString:@"f2f2f1"];
    return sectionView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJ_HomeNewsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"XJ_HomeNewsCell"];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"XJ_HomeNewsCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    cell.contentView.backgroundColor = [UIColor colorWithHexString:@"f2f2f1"];

    cell.clipsToBounds = YES;
    cell.backView.layer.cornerRadius = 10;
    XJ_homeNewListModel * model = [self.newsArray objectAtIndex:indexPath.row];
    
    cell.timeLabel.text = model.xiaoxiDate;
    
    cell.contentLabel.text = model.content;
    cell.contentLabel.numberOfLines = 0;
    
    return cell;
}

- (void)requestNewsListData{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    
    [XJ_Utils signPost:@"home/news?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        NSArray * arr = [dataDic objectForKey:@"newList"];
        
        if (arr.count > 0) {
            
            for (NSDictionary * dic in arr) {
                
                XJ_homeNewListModel * newModel = [XJ_homeNewListModel mj_objectWithKeyValues:dic];
                
                newModel.xiaoxiDate = [dic objectForKey:@"newDate"];
                [self.newsArray addObject:newModel];
            }
        }
        
        [self.newTabel reloadData];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
    
}

- (void)backButtonClick{
    
    if(_isPush == YES){
        
//        [self dismissViewControllerAnimated:YES completion:^{
//            
//        }];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
